<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属二级学科id" prop="secondSubjectId">
        <el-input
          v-model="queryParams.secondSubjectId"
          placeholder="请输入所属二级学科id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属学院id" prop="collegeId">
        <el-input
          v-model="queryParams.collegeId"
          placeholder="请输入所属学院id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="研究方向名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入研究方向名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="研究类型" prop="researchType">
        <el-select v-model="queryParams.researchType" placeholder="请选择研究类型" clearable>
          <el-option
            v-for="dict in research_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="研究方向关键词(逗号分隔)" prop="researchKeywords">
        <el-input
          v-model="queryParams.researchKeywords"
          placeholder="请输入研究方向关键词(逗号分隔)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="方向负责人ID" prop="leaderId">
        <el-input
          v-model="queryParams.leaderId"
          placeholder="请输入方向负责人ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预算金额" prop="budget">
        <el-input
          v-model="queryParams.budget"
          placeholder="请输入预算金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="研究起始日期" prop="startDate">
        <el-date-picker clearable
          v-model="queryParams.startDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择研究起始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="研究结束日期" prop="endDate">
        <el-date-picker clearable
          v-model="queryParams.endDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择研究结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态：1(启用),0(停用)" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态：1(启用),0(停用)" clearable>
          <el-option
            v-for="dict in use_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="版本号" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人id" prop="createId">
        <el-input
          v-model="queryParams.createId"
          placeholder="请输入创建人id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新人id" prop="updateId">
        <el-input
          v-model="queryParams.updateId"
          placeholder="请输入更新人id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['college:researchDirection:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['college:researchDirection:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['college:researchDirection:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['college:researchDirection:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="researchDirectionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一标识" align="center" prop="id" />
      <el-table-column label="所属二级学科id" align="center" prop="secondSubjectId" />
      <el-table-column label="所属学院id" align="center" prop="collegeId" />
      <el-table-column label="研究方向名称" align="center" prop="name" />
      <el-table-column label="研究类型" align="center" prop="researchType">
        <template #default="scope">
          <dict-tag :options="research_type" :value="scope.row.researchType"/>
        </template>
      </el-table-column>
      <el-table-column label="研究方向关键词(逗号分隔)" align="center" prop="researchKeywords" />
      <el-table-column label="研究方向简介" align="center" prop="description" />
      <el-table-column label="附件存储路径" align="center" prop="attachmentPath" />
      <el-table-column label="方向负责人ID" align="center" prop="leaderId" />
      <el-table-column label="预算金额" align="center" prop="budget" />
      <el-table-column label="研究起始日期" align="center" prop="startDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="研究结束日期" align="center" prop="endDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态：1(启用),0(停用)" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="use_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="版本号" align="center" prop="version" />
      <el-table-column label="创建人id" align="center" prop="createId" />
      <el-table-column label="更新人id" align="center" prop="updateId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['college:researchDirection:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['college:researchDirection:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学科研究方向对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="researchDirectionRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属二级学科id" prop="secondSubjectId">
          <el-input v-model="form.secondSubjectId" placeholder="请输入所属二级学科id" />
        </el-form-item>
        <el-form-item label="所属学院id" prop="collegeId">
          <el-input v-model="form.collegeId" placeholder="请输入所属学院id" />
        </el-form-item>
        <el-form-item label="研究方向名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入研究方向名称" />
        </el-form-item>
        <el-form-item label="研究类型" prop="researchType">
          <el-select v-model="form.researchType" placeholder="请选择研究类型">
            <el-option
              v-for="dict in research_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="研究方向关键词(逗号分隔)" prop="researchKeywords">
          <el-input v-model="form.researchKeywords" placeholder="请输入研究方向关键词(逗号分隔)" />
        </el-form-item>
        <el-form-item label="研究方向简介" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="附件存储路径" prop="attachmentPath">
          <file-upload v-model="form.attachmentPath"/>
        </el-form-item>
        <el-form-item label="方向负责人ID" prop="leaderId">
          <el-input v-model="form.leaderId" placeholder="请输入方向负责人ID" />
        </el-form-item>
        <el-form-item label="预算金额" prop="budget">
          <el-input v-model="form.budget" placeholder="请输入预算金额" />
        </el-form-item>
        <el-form-item label="研究起始日期" prop="startDate">
          <el-date-picker clearable
            v-model="form.startDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择研究起始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="研究结束日期" prop="endDate">
          <el-date-picker clearable
            v-model="form.endDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择研究结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态：1(启用),0(停用)" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态：1(启用),0(停用)">
            <el-option
              v-for="dict in use_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="版本号" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="创建人id" prop="createId">
          <el-input v-model="form.createId" placeholder="请输入创建人id" />
        </el-form-item>
        <el-form-item label="更新人id" prop="updateId">
          <el-input v-model="form.updateId" placeholder="请输入更新人id" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ResearchDirection">
import { listResearchDirection, getResearchDirection, delResearchDirection, addResearchDirection, updateResearchDirection } from "@/api/college/researchDirection";

const { proxy } = getCurrentInstance();
const { use_status, research_type } = proxy.useDict('use_status', 'research_type');

const researchDirectionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    secondSubjectId: null,
    collegeId: null,
    name: null,
    researchType: null,
    researchKeywords: null,
    description: null,
    attachmentPath: null,
    leaderId: null,
    budget: null,
    startDate: null,
    endDate: null,
    status: null,
    version: null,
    createId: null,
    updateId: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询学科研究方向列表 */
function getList() {
  loading.value = true;
  listResearchDirection(queryParams.value).then(response => {
    researchDirectionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    secondSubjectId: null,
    collegeId: null,
    name: null,
    researchType: null,
    researchKeywords: null,
    description: null,
    attachmentPath: null,
    leaderId: null,
    budget: null,
    startDate: null,
    endDate: null,
    status: null,
    version: null,
    createId: null,
    updateId: null,
    createTime: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("researchDirectionRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加学科研究方向";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getResearchDirection(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改学科研究方向";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["researchDirectionRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateResearchDirection(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addResearchDirection(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除学科研究方向编号为"' + _ids + '"的数据项？').then(function() {
    return delResearchDirection(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('college/researchDirection/export', {
    ...queryParams.value
  }, `researchDirection_${new Date().getTime()}.xlsx`)
}

getList();
</script>
